package com.duanjw.springcloud.oss;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.juhewu.oss.spring.boot.core.OssTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3ObjectSummary;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;

/**
 * 文件上传、下载
 *
 * @author duanjw
 */
@RestController
@AllArgsConstructor
public class FileController {

    private final OssTemplate template;

    /**
     * 新增 bucket
     * curl -X POST localhost:7004/bucket/100
     */
    @SneakyThrows
    @PostMapping("/bucket/{bucket}")
    public Bucket insertBucket(@PathVariable String bucket) {
        template.createBucket(bucket);
        return template.getBucket(bucket).orElseThrow(NullPointerException::new);
    }

    /**
     * 查询所有 bucket
     * curl localhost:7004/bucket
     *
     * @return 所有 bucket
     */
    @SneakyThrows
    @GetMapping("/bucket")
    public List<Bucket> selectBucket() {
        return template.getAllBuckets();
    }

    /**
     * 指定bucket删除bucket
     * curl -X DELETE localhost:7004/bucket/100
     *
     * @param bucketName bucketName
     */
    @SneakyThrows
    @DeleteMapping("/bucket/{bucketName}")
    public Boolean deleteBucket(@PathVariable String bucketName) {
        template.removeBucket(bucketName);
        return true;
    }

    /**
     * 在指定的bucket上传文件
     * <p>
     * 生成临时文件：dd if=/dev/zero of=/tmp/test.iso bs=1 count=1
     * curl localhost:7004/file/bucket/100 -F "file=@/tmp/test.iso"
     */
    @SneakyThrows
    @PostMapping("/file/bucket/{bucketName}")
    public Boolean uploadFile(@RequestBody MultipartFile file, @PathVariable String bucketName) {
        String filename = file.getOriginalFilename();
        template.putObject(bucketName, filename, file.getInputStream(), file.getSize(), file.getContentType());
        return true;
    }

    /**
     * 在指定的bucket中查询文件
     * curl localhost:7004/file/bucket/100/filename/test.iso
     *
     * @param bucketName
     * @param filename
     * @return
     */
    @SneakyThrows
    @GetMapping("/file/bucket/{bucketName}/filename/{filename}")
    public List<S3ObjectSummary> selectFile(@PathVariable String bucketName, @PathVariable String filename) {
        return template.getAllObjectsByPrefix(bucketName, filename);
    }

    /**
     * 在指定的bucket下载文件
     * curl localhost:7004/file/test.iso/bucket/100
     */
    @GetMapping("/file/{fileName}/bucket/{bucketName}")
    public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName,
            @PathVariable("bucketName") String bucketName) {
        FileUtil.download(request, response, template.getObject(bucketName, fileName).getObjectContent(), fileName);
    }

    /**
     * 在指定的bucket删除文件
     *
     * @param bucketName 桶名
     * @param filename 文件名
     */
    @SneakyThrows
    @DeleteMapping("/file/bucket/{bucketName}/filename/{filename}")
    public Boolean deleteFile(@PathVariable String bucketName, @PathVariable String filename) {
        template.removeObject(bucketName, filename);
        return true;
    }
}
